class AuditObserver < ActiveRecord::Observer
  observe :group,:branch,:model,:permission,:authentication,:customer,:title,:card,:nationality,:province,:occupation,:agreement
  
  def after_create(record)
    audit = Audit.new
    audit.model=record.class.to_s
    audit.action = 'Create'
    audit.after = record.to_audit
    if record.user_log.nil?
      audit.user = 'initial'
    else
      audit.user = record.user_log
    end
    audit.save
  end
  
  def before_update(record)
    c = record.class
    obj = c.find(record.id)
    
    audit = Audit.new
    audit.model=record.class.to_s
    audit.action = 'Update'
    audit.before = obj.to_audit
    audit.save
    @id = audit.id
  end
  
  def after_update(record)
    audit = Audit.find(@id)
    audit.after = record.to_audit
    if record.user_log.nil?
      audit.user = 'initial'
    else
      audit.user = record.user_log
    end
    audit.save
  end
  
  def after_destroy(record)
    audit = Audit.new
    audit.model=record.class.to_s
    audit.action = 'Destroy'
    audit.before = record.to_audit
    audit.user = record.user_log
    audit.save
  end
  
end